$(function () {
    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var all_datagrid =  $('#all_datagrid');

    //数据表格
    all_datagrid.datagrid({
        url:'/bigClient/list.do',
        title:"大客户管理",
        striped:true,
        singleSelect:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'schoolName',title:'学校名称',width:100,formatter:schoolFormatter},
            {field:'important',title:'重要程度',width:100,formatter:importantFormatter},
            {field:'ittState',title:'意向程度',width:100,formatter:ittStateFormatter},
            {field:'ittSubject',title:'意向学科',width:100,formatter:ittSubjectFormatter},
            {field:'phone',title:'电话',width:100},
            {field:'linkman',title:'联系人',width:100,formatter:linkmanFormatter},
            {field:'marketer',title:'营销人员',width:100,formatter:marketerFormatter},
            {field:'tracer',title:'跟进人员',width:100,formatter:tracerFormatter},
            {field:'lastFollowTime',title:'最后跟踪时间',width:100},
            {field:'nextFollowTime',title:'下次跟踪时间',width:100},
            {field:'followState',title:'跟进状态',width:100,formatter:followStateFormatter},
            {field:'contractState',title:'合同状态',width:100,formatter:contractState}
        ]]
    });
    //end



    var cmdObj = {

        //签约
        sign:function(){
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要签约的客户","info");
                return;
            }

            if (row.contractState){
                $.messager.alert("温馨提示","该用户已经签约","info");
                return;
            }


            $.messager.confirm('确认','确认要进行签约操作吗吗？',function(r){
                if (r){
                    $.get("/bigClient/sign.do?id="+row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','签约完成',function(r){
                                all_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            })
        },
        //查看按钮
        watch:function(){
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要查看的客户","info");
                return;
            }

            edit_dialog.dialog("setTitle","查看看客户");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            $(".readOnly").textbox("readonly",true);


            if(row.linkman){
                row["linkman.id"]=row.linkman.id;
            }

            if(row.campus){
                row["campus.id"]=row.campus.id;
            }


            if(row.schoolName){
                row["schoolName.id"]=row.schoolName.id;
            }
            if(row.important){
                row["important.id"]=row.important.id;
            }
            if(row.ittState){
                row["ittState.id"]=row.ittState.id;
            }
            if(row.ittSubject){
                row["ittSubject.id"]=row.ittSubject.id;
            }

            if(row.marketer){
                row["marketer.id"]=row.marketer.id;
            }
            if(row.tracer){
                row["tracer.id"]=row.tracer.id;
            }

            edit_form.form("load",row);

            //隐藏按钮
            $('.dialog-button a:eq(0)').hide();
            $('.dialog-button a:eq(1)').hide();

            edit_dialog.dialog("open");
        },

        //重置
        clear:function(){
            edit_form.form("clear");
        },

        query : function () {
            var param = $("#keyword").val();
            var ittSubjectId = $("#ittSubjectId").val();

            all_datagrid.datagrid('load',{
                keyword: param,
                ittSubjectId: ittSubjectId
            });
        },

        //点击添加按钮,添加客户学校
        add : function () {
            edit_dialog.dialog("setTitle","添加客户学校");
            edit_form.form("clear");

            //显示按钮
            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();

            edit_dialog.dialog("open");
        },
        //刷新
        reload : function () {
            all_datagrid.datagrid("reload");
        },
        //编辑按钮编辑客户学校
        edit : function () {

            //显示按钮
            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();

            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的客户学校","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑客户学校");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            if(row.linkman){
                row["linkman.id"]=row.linkman.id;
            }
            if(row.schoolName){
                row["schoolName.id"]=row.schoolName.id;
            }
            if(row.important){
                row["important.id"]=row.important.id;
            }
            if(row.ittState){
                row["ittState.id"]=row.ittState.id;
            }
            if(row.ittSubject){
                row["ittSubject.id"]=row.ittSubject.id;
            }
            if(row.marketer){
                row["marketer.id"]=row.marketer.id;
            }
            if(row.tracer){
                row["tracer.id"]=row.tracer.id;
            }


            edit_form.form("load",row);

            edit_dialog.dialog("open");
        },
        //save按钮提交表单
        save : function () {


            var url = "";

            var id = $("#hiddenId").val();

            if(id){
                url = "/bigClient/update.do";
            }else{
                url = "/bigClient/save.do";
            }

            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            all_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //删除客户学校
        remove : function () {
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的客户学校","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/bigClient/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                all_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },

        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        }
    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end

    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: "60%",
        height: "80%"
    });
    //end


})//end

//重要程度格式化
function importantFormatter(value,row,index) {
    if (value){
        return value.name;
    }
}
//意向学科格式化
function ittSubjectFormatter(value,row,index) {
    if (value){
        return value.name;
    }
}
//学校名称格式化
function schoolFormatter(value,row,index) {
    if (value){
        return value.name;
    }
}
//意向程度格式化
function ittStateFormatter(value,row,index) {
    if (value){
        return value.name;
    }
}
//营销人员格式化格式化
function marketerFormatter(value,row,index) {
    if (value){
        return value.username;
    }
}
//跟进人员格式化格式化
function tracerFormatter(value,row,index) {
    if (value){
        return value.username;
    }
}
//客户学校格式化格式化
function linkmanFormatter(value,row,index) {
    if (value){
        return value.name;
    }
}

function followStateFormatter(value,row,index) {
    if (value){
        return "<span style='color: green'>已跟进</span>"
    } else{
        return "<span style='color: red'>未跟进</span>"
    }
}
function contractState(value,row,index) {
    if (value){
        return "<span style='color: green'>已签约</span>"
    } else{
        return "<span style='color: red'>未签约</span>"
    }
}